package ui;

import dao.DataManager;
import java.awt.*;
import java.util.List;
import java.util.Random;
import javax.swing.*;
import model.Group;
import model.Student;

public class RandomCallPanel extends JPanel {
    private JLabel selectedLabel;
    private JComboBox<Group> groupComboBox;
    private Random random;
    
    public RandomCallPanel() {
        setLayout(new BorderLayout(10, 10));
        random = new Random();
        initComponents();
    }
    
    private void initComponents() {
        // 创建控制面板
        JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
        
        // 创建小组选择下拉框
        groupComboBox = new JComboBox<>();
        updateGroupComboBox();
        
        // 创建按钮
        JButton randomGroupButton = new JButton("随机抽取小组");
        JButton randomGroupStudentButton = new JButton("小组内随机抽人");
        JButton randomStudentButton = new JButton("随机抽取学生");
        
        // 添加到控制面板
        controlPanel.add(randomGroupButton);
        controlPanel.add(groupComboBox);
        controlPanel.add(randomGroupStudentButton);
        controlPanel.add(randomStudentButton);
        
        // 创建显示面板
        JPanel displayPanel = new JPanel();
        displayPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        selectedLabel = new JLabel("等待抽取...");
        selectedLabel.setFont(new Font("宋体", Font.BOLD, 32));
        displayPanel.add(selectedLabel);
        
        // 添加到主面板
        add(controlPanel, BorderLayout.NORTH);
        add(displayPanel, BorderLayout.CENTER);
        
        // 添加事件监听
        randomGroupButton.addActionListener(e -> randomSelectGroup());
        randomGroupStudentButton.addActionListener(e -> randomSelectStudentInGroup());
        randomStudentButton.addActionListener(e -> randomSelectStudent());
    }
    
    private void updateGroupComboBox() {
        groupComboBox.removeAllItems();
        List<Group> groups = DataManager.getInstance().getAllGroups();
        for (Group group : groups) {
            groupComboBox.addItem(group);
        }
    }
    
    private void randomSelectGroup() {
        List<Group> groups = DataManager.getInstance().getAllGroups();
        if (groups.isEmpty()) {
            JOptionPane.showMessageDialog(this, "还没有创建任何小组！");
            return;
        }
        
        int randomIndex = random.nextInt(groups.size());
        Group selectedGroup = groups.get(randomIndex);
        
        selectedLabel.setText(String.format("抽中小组：%s", selectedGroup.getName()));
        // 自动选中抽中的小组
        groupComboBox.setSelectedItem(selectedGroup);
    }
    
    private void randomSelectStudentInGroup() {
        Group selectedGroup = (Group) groupComboBox.getSelectedItem();
        if (selectedGroup == null) {
            JOptionPane.showMessageDialog(this, "请先选择一个小组！");
            return;
        }
        
        List<Student> students = DataManager.getInstance().getStudentsByGroup(selectedGroup.getId());
        if (students.isEmpty()) {
            JOptionPane.showMessageDialog(this, "该小组中没有学生！");
            return;
        }
        
        int randomIndex = random.nextInt(students.size());
        Student selectedStudent = students.get(randomIndex);
        
        selectedLabel.setText(String.format("抽中学生：%s（%s小组）", 
            selectedStudent.getName(), selectedGroup.getName()));
    }
    
    private void randomSelectStudent() {
        List<Student> allStudents = DataManager.getInstance().getAllStudents();
        if (allStudents.isEmpty()) {
            JOptionPane.showMessageDialog(this, "还没有添加任何学生！");
            return;
        }
        
        int randomIndex = random.nextInt(allStudents.size());
        Student selectedStudent = allStudents.get(randomIndex);
        
        // 获取学生所在的小组（如果有）
        String groupInfo = "";
        if (selectedStudent.getGroupId() != 0) {
            Group group = DataManager.getInstance().getGroup(selectedStudent.getGroupId());
            if (group != null) {
                groupInfo = String.format("（%s小组）", group.getName());
            }
        }
        
        selectedLabel.setText(String.format("抽中学生：%s%s", 
            selectedStudent.getName(), groupInfo));
    }
} 